package site.luojie.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import site.luojie.pojo.Article;
import site.luojie.service.ArticleService;

/**
 * @Description: 文章controller
 * @Author jie.luo
 * @Create: 2020-06-02 22:43
 **/
@Controller
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping("/articles")
    @ResponseBody
    public Page<Article> page(Integer page, Integer size) {
        return articleService.page(page, size);
    }

    @RequestMapping("/index")
    public String toIndex(Integer page, Integer size, Model model) {

        if (page < 0) {
            page = 0;
        }

        Page<Article> articlePage = articleService.page(page, size);

        model.addAttribute("articles", articlePage.getContent());
        model.addAttribute("page", articlePage.getPageable().getPageNumber());
        model.addAttribute("size", articlePage.getPageable().getPageSize());
        model.addAttribute("totalPage", articlePage.getTotalPages());

        return "client/index";
    }

}
